package cn.b.sky.mes.em.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.em.dao.EquipmentOutputDao;
import cn.b.sky.mes.em.model.EquipmentOutput;
import cn.b.sky.mes.em.model.condition.EquipmentOutputCondition;
import cn.b.sky.mes.em.service.EquipmentOutputService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("equipmentOutputService")
public class EquipmentOutputServiceImpl implements EquipmentOutputService {

  private static final Logger LOG = LoggerFactory.getLogger(EquipmentOutputServiceImpl.class);

  @Resource
  EquipmentOutputDao outputDao;

  @Override
  public List<EquipmentOutput> selectEquipmentOutputListByCondition(EquipmentOutputCondition condition) {
    if (condition == null) {
      condition = new EquipmentOutputCondition();
    }
    if (condition.getOrderField() == null) {
      condition.setOrderField("CreateDateTime");
    }
    return outputDao.selectEquipmentOutputListByCondition(condition);
  }

  @Override
  public int selectEquipmentOutputCountByCondition(EquipmentOutputCondition condition) {
    if (condition == null) {
      condition = new EquipmentOutputCondition();
    }
    return outputDao.selectEquipmentOutputCountByCondition(condition);
  }

  @Override
  public EquipmentOutput getEquipmentOutput(Long id) {
    if (id == null) {
      return null;
    }
    return outputDao.getEquipmentOutput(id);
  }

  @Override
  public boolean insertEquipmentOutput(EquipmentOutput output) {
    if (StringUtils.isBlank(output.getEquipmentCode())) {
      return false;
    }
    outputDao.insertEquipmentOutput(output);
    return true;
  }

  @Override
  public boolean updateEquipmentOutput(EquipmentOutput output) {
    if (StringUtils.isBlank(output.getEquipmentCode()) || output.getId() == null) {
      return false;
    }
    outputDao.updateEquipmentOutput(output);
    return true;
  }

  @Override
  public BatchJobReport deleteEquipmentOutPut(Long id) {
    if (id != null) {
      String message = "";
      String cause = "";
      boolean success = true;
      try {
        outputDao.deleteEquipmentOutPut(id);
      } catch (Exception e) {
        LOG.error("{}", e);
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(e.getMessage())) {
          //外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
              SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                  SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        message = this.getEquipmentOutput(id).getEquipmentCode();
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success);
    } else {
      return new BatchJobReport("", "equipmentCode is null", false);
    }
  }

}
